// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue';
import App from './App';
import router from './router';
import axios from 'axios';
Vue.prototype.$http = axios;
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import store from './store'; // 应用vuex
import moment from 'moment'; //时间格式化
import '../theme/index.css';
import Validate from './../src/utils/validate';
import './../static/css/common.css';
import Blob from './vendor/Blob';
import Export2Excel from './vendor/Export2Excel';
//导入过滤器
import './filter';
Vue.use(Validate);
import {
  ddlwp
} from 'ddlwp';

import {
  dingTalkPc
} from 'cnf-dingtalk-auth';

Vue.use(ElementUI, {
  size: 'small',
});
Vue.config.productionTip = false;

// 时间格式化
Vue.prototype.$moment = function (value, format) {
  if (!value) {
    return '';
  }
  return moment(Number(value)).format(format || 'YYYY-MM-DD HH:mm');
};
Vue.prototype.$pageSizes = [10, 20, 30, 40, 50, 100, 200, 500]; //分页大小
Vue.prototype.$openPage = function (path, query, way = 'push') {
  router[way]({
    path: path,
    query: {
      ...query,
    },
  });
};

localStorage.setItem(
  'corpId',
  decodeURIComponent(
    (new RegExp('[?|&]' + 'corpId' + '=' + '([^&;]+?)(&|#|;|$)').exec(
      location.href
    ) || [, ''])[1]
    .replace(/\+/g, '%20')
  ) || ''
);
localStorage.setItem(
  'appkey',
  decodeURIComponent(
    (new RegExp('[?|&]' + 'appkey' + '=' + '([^&;]+?)(&|#|;|$)').exec(
      location.href
    ) || [, ''])[1]
    .replace(/\+/g, '%20')
  ) || ''
);

const way = '2'; //1之前 2ddlwp

if (way == '1') {
  const jsListApi = ['biz.contact.choose', 'biz.util.uploadImage'];
  dingTalkPc.ddConfig(jsListApi, '/api/isv/auth/getJsConfig').then(
    res => {
      // isvPC签名
      DingTalkPC.runtime.permission.requestAuthCode({
        // 免登
        corpId: localStorage.getItem('corpId'),
        onSuccess: function (result) {
          let authUrl =
            '/api/isv/auth/login' +
            ('?corpId=' +
              localStorage.getItem('corpId') +
              '&code=' +
              result.code +
              '&appkey=' +
              localStorage.getItem('appkey'));
          axios.get(authUrl).then(
            function (res) {
              if (res.data.success) {
                store.state.activityId.myPower = res.data.result.qx;
                // store.state.activityId.myPower = 3;
                axios.defaults.headers.common['Authorization'] =
                  res.data.result.token; //请求头文件
                new Vue({
                  el: '#app',
                  router,
                  store,
                  components: {
                    App,
                  },
                  template: '<App/>',
                });
              } else {
                ElementUI.Message.warning({
                  message: res.data.errorMsg || `用户信息获取失败`,
                  type: 'warning',
                });
              }
            },
            function (err) {
              ElementUI.Message.warning({
                message: `免登失败`,
                type: 'warning',
              });
            }
          );
        },
        onFail: function (err) {
          ElementUI.Message.warning({
            message: `获取临时授权码失败`,
            type: 'warning',
          });
        },
      });
    },
    err => {
      ElementUI.Message.warning({
        message: err.errorMessage || `签名失败`,
        type: 'warning',
      });
    }
  );
} else {
  // 改成lwp方式不要签名，有此方式免登
  ddlwp(localStorage.getItem('corpId')).then(
    res => {
      console.log('ddlwp', res);
      axios.defaults.headers.common['Authorization'] = res.result.token; //请求头文件
      if (res.success) {
        queryRbacNewVue();
      } else {
        ElementUI.Message.warning({
          message: res.errorMsg || '用户信息获取失败',
          type: 'warning',
        });
      }
    },
    err => {
      ElementUI.Message.warning({
        message: err,
        type: 'warning',
      });
    }
  );
  async function queryRbacNewVue() {
    let res = await queryRbac();
    new Vue({
      el: '#app',
      router,
      store,
      components: {
        App,
      },
      template: '<App/>',
    });
  }

  function queryRbac() {
    return new Promise(resolve => {
      axios.get('/api/isv/rbac/queryRbac').then(
        res => {
          if (res.data.success) {
            store.state.activityId.myPower = res.data.result.qx;
            resolve(res.data.result.qx);
          } else {
            resolve(4);
          }
        },
        err => {
          resolve(4);
        }
      );
    });
  }
}
// new Vue({
//   el: '#app',
//   router,
//   store,
//   components: {
//     App
//   },
//   template: '<App/>',
// })
